--转盼散流光
local zhuanpansanliuguang = fk.CreateSkill {
    name = "mcdh_zhuanpansanliuguang",
}
local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

zhuanpansanliuguang:addEffect(fk.DrawNCards,{
    can_trigger = function(self, event, target, player, data)
        if event == fk.DrawNCards then
            return player:hasSkill(self) and target == player
        else
            return target == player and (data.extra_data or {}).zhuanpansanliuguang ~= nil
        end

    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local choices = {"zhuanpansanliuguang1","zhuanpansanliuguang2","Cancel"}
        if data.n  == 0 then
                table.removeOne(choices,"zhuanpansanliuguang1")
        end
        local  choice = room:askForChoice(player,  choices , self.name, nil,nil,  {"zhuanpansanliuguang1","zhuanpansanliuguang2","Cancel"})
        if choices  and choice ~= "Cancel" then
                self.cost_data = choice
                data.extra_data = data.extra_data or {}
                data.extra_data.zhuanpansanliuguang = data.extra_data.zhuanpansanliuguang or {}
                table.insertIfNeed(data.extra_data.zhuanpansanliuguang,choice)
                return true
        end

    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
            if self.cost_data == "zhuanpansanliuguang1" then
                data.n = data.n -1
            elseif self.cost_data == "zhuanpansanliuguang2" then
                data.n = data.n + 1
            end
    end
})

zhuanpansanliuguang:addEffect(fk.AfterDrawNCards,{
    can_trigger = function(self, event, target, player, data)
            return target == player and (data.extra_data or {}).zhuanpansanliuguang ~= nil

    end,
    on_cost = function(self, event, target, player, data)
            return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
            if data.extra_data.zhuanpansanliuguang[1] == "zhuanpansanliuguang1" then
                local p = room:askToChoosePlayers(player,{
                    targets = room:getOtherPlayers(player),
                    min_num = 1,
                    max_num = 1,
                    prompt = "#zhuanpansanliuguang-choose",
                    skill_name = zhuanpansanliuguang.name,
                    cancelable = false,
                })[1]

                p:drawCards(2,self.name)

                if not p:isNude() then
                    local card = room:askToCards(p, {
                        min_num = 1,
                        max_num = 1,
                        skill_name = zhuanpansanliuguang.name,
                        cancelable = true,
                        prompt = "#zhuanpansanliuguang_other-give:"..player.id,
                    })

                    room:moveCards({
                        ids = card ,
                        from = p,
                        to = player,
                        toArea = Card.PlayerHand,
                        moveReason = fk.ReasonGive,
                        skillName =  self.name,
                        moveVisible = false
                    })
                end
            elseif data.extra_data.zhuanpansanliuguang[1] == "zhuanpansanliuguang2" then
                if not player:isNude() then
                    local tor,card =  room:askToChooseCardsAndPlayers(player, {
                        targets = room:getOtherPlayers(player),
                        min_card_num = 1,
                        max_card_num = 1,
                        min_num = 1 ,
                        max_num = 1 ,
                        prompt = "#zhuanpansanliuguang-give",
                        skill_name = zhuanpansanliuguang.name,
                        cancelable = false,
                    })

                    room:moveCards({
                        ids = card ,
                        from = player,
                        to = tor[1],
                        toArea = Card.PlayerHand,
                        moveReason = fk.ReasonGive,
                        skillName =  self.name,
                        moveVisible = false
                    })
                end
            end
    end
})

Fk:loadTranslationTable {
    ["mcdh_zhuanpansanliuguang"] = "流光",
    [":mcdh_zhuanpansanliuguang"] = "『转盼散流光』<br>摸牌阶段，你可以选择一项：1.少摸一张牌，令一名其他角色摸两张牌，然后其可以交给你一张牌。2.多摸一张牌，然后交给一名其他角色一张牌。",
    ["zhuanpansanliuguang1"] = "少摸一张牌，令一名其他角色摸两张牌，然后其可以交给你一张牌。",
    ["zhuanpansanliuguang2"] = "多摸一张牌，然后交给一名其他角色一张牌。",
    ["#zhuanpansanliuguang-give"] =  "转盼散流光：交给一名其他角色一张牌",
    ["#zhuanpansanliuguang-choose"] ="转盼散流光：令一名其他角色摸两张牌",
    ["#zhuanpansanliuguang_other-give"] ="转盼散流光：交给 %src 一张牌",
}

return zhuanpansanliuguang